import html2pdf from "html2pdf.js";
import {ElLoading} from "element-plus";

export var exportAndDownloadPdf = (filename,element,callback) => {
    let loadingInstance = ElLoading.service({
        lock: true,
        text: '正在生成，请稍后。。。',
        spinner: 'el-icon-loading',
    })

    const scale = window.devicePixelRatio
    const opt = {
        margin: 1,
        filename: filename,
        html2canvas: {scale},
        jsPDF: {unit: 'in', format: 'letter', orientation: 'portrait'}
    }
    html2pdf()
        .set(opt)
        .from(element)
        .save()
        .then(() => {
            loadingInstance.close()
            if (callback){
                callback()
            }
        })
};

const download = ( content, filename) => {
    const aElement = document.createElement("a");
    if ("download" in aElement) {
        aElement.download = filename;
        aElement.style.display = "none";
        aElement.href = URL.createObjectURL(new Blob([content]));

        document.body.appendChild(aElement);
        aElement.click();
        aElement.remove();
    }
};


export var exportMarkdown = (markdownText ,filename) => {
    download( markdownText, filename);
};

